﻿using App.Chat.Application.Interface;
using App.Chat.Auth;
using App.Chat.Contract;
using App.Chat.Contract.Input;
using App.Chat.Contract.Ouput;
using App.Chat.Models;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Zws.Redis;

namespace App.Chat.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : BaseController
    {
        private readonly IMapper _mapper;
        private readonly RedisClient _redisClient;
        private readonly IUserService _userService;
        public UserController(CurrentUser currentUser,
            IMapper mapper,
            RedisClient redisClient,
            IUserService userService) : base(currentUser)
        {
            _mapper = mapper;
            _redisClient = redisClient;
            _userService = userService;
        }
        [HttpPost("register")]
        public async Task<UserLoginOuput> RegisterAsync(UserAddInput input)
        {
            var user = _mapper.Map<User>(input);
            var token = await _userService.RegisterAsync(user);
            UserLoginOuput output = UserResultOutput(user, token);
            return output;
        }
        [HttpPost("login")]
        public async Task<UserLoginOuput> LoginAsync(UserAddInput input)
        {
            var user = _mapper.Map<User>(input);
            var (findUser, token) = await _userService.LoginAsync(user);
            await CacheUser2Redis(findUser, token);
            var output = UserResultOutput(findUser, token);
            return output;
        }

        private async Task CacheUser2Redis(User findUser, string token)
        {
            var userCache = _mapper.Map<UserCacheOutput>(findUser);
            await _redisClient.SetByKeyAsync(token, userCache);
        }

        private UserLoginOuput UserResultOutput(User user, string token)
        {
            var output = _mapper.Map<UserLoginOuput>(user);
            output.token = token;
            return output;
        }

        /// <summary>
        /// 用户查询
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        [NeedAuth]
        [HttpGet("findUsers")]
        public async Task<List<UserListOutput>> FindUsersAsync(string userName)
        {
            var users = await _userService.FindUsersAsync(_currentUser.Id, userName);
            var outputList = _mapper.Map<List<UserListOutput>>(users);
            return outputList;
        }
        [NeedAuth]
        [HttpGet("updateNickName")]
        public async Task<string> UpdateNickNameAsync(string nickName)
        {
            var user = await _userService.UpdateNickNameAsync(_currentUser.Id, nickName);
            //更新缓存
            await CacheUser2Redis(user, _currentUser.Token);
            return "success";
        }
        [NeedAuth]
        [HttpPost("changePwd")]
        public async Task<string> ChangePwdAsync(ChangePwdInput input)
        {
            await _userService.ChangePwdAsync(_currentUser.Id, input.OldPwd, input.NewPwd);
            //清楚旧缓存，让用户重新登录
            await _redisClient.DeleteAsync(_currentUser.Token);
            return "success";
        }
        [NeedAuth]
        [HttpPost("getUserInfo")]
        public async Task<UserCacheOutput> GetUserInfoAsync()
        {
            return await _redisClient.GetByKeyAsync<UserCacheOutput>(_currentUser.Token);
        }
    }
}
